/*
 * Instructores.java
 *
 * Created on 10 de marzo de 2008, 01:46 PM
 */

package swimsystem;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.util.logging.Logger;
import java.util.logging.Level;


/**
 *
 * @author  Edwarck Static
 */
public class Instructores extends javax.swing.JFrame {
   
    /** Creates new form Instructores */
    cInstructores miContro_instructores;
    public Instructores() {
        miContro_instructores=new cInstructores(this);
          
        initComponents();
        
        crearConexion();
    }
    
    
    static Connection con;
    static Statement stat;
    static ResultSet rs;
    static String error;
    
    static void crearConexion(){
        try {
            Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
            //Class.forName( "org.gjt.mm.mysql.Driver" );
        } catch( Exception e ) {
            JOptionPane.showMessageDialog(null,"No se pudo cargar el puente JDBC-ODBC.");
        }
        
        try {
          // Se establece la conexión con la base de datos
          con = DriverManager.getConnection("jdbc:mysql://localhost/Instructores","root", "Abc321");
          
          stat = con.createStatement();
        } catch( Exception e ) {
          System.out.println( e );
        }
        
    }
       
    
    public String getNombre(){
       return txtExperiencia.getText();
    }
    public String getApellido_paterno(){
        return txtExperiencia.getText();
    }
    public String getApellido_materno(){
        return txtExperiencia.getText();
    }
    public String getInstituciones(){
        return txtExperiencia.getText();
    }
    public String getDireccion(){
        return txtExperiencia.getText();
    }
    public String getColonia(){
        return txtExperiencia.getText();
    }
    public String getExperiencia(){
        return txtExperiencia.getText();
    }
    public String getTelefono(){
        return txtExperiencia.getText();
    }
    
    public void setNombre(String nombre){
        
    }
    public void setApellido_paterno(String apellido_paterno){
        
    }
    public void setApellido_materno(String apellido_materno){
        
    }
    public void setInstituciones(String ciudad){
        
    }
    public void setDireccion(String municipio){
        
    }
    public void setColonia(String colonia){
        
    }
    public void setExperiencia(String fechaNacimiento){
        
    }
    public void setTelefono(String telCasa){
        
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtApPat = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtApMat = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtInstituciones = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtColonia = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtTel = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtDir = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtExperiencia = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        lblEstado = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        pnlContenedor = new javax.swing.JPanel();
        btnMostrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setLayout(new java.awt.GridLayout(8, 0));

        jLabel1.setText("Nombre");
        jPanel1.add(jLabel1);
        jPanel1.add(txtNombre);

        jLabel2.setText("Apellido Paterno");
        jPanel1.add(jLabel2);
        jPanel1.add(txtApPat);

        jLabel3.setText("Apellido Materno");
        jPanel1.add(jLabel3);
        jPanel1.add(txtApMat);

        jLabel9.setText("Instituciones anteriores");
        jPanel1.add(jLabel9);
        jPanel1.add(txtInstituciones);

        jLabel6.setText("Colonia");
        jPanel1.add(jLabel6);
        jPanel1.add(txtColonia);

        jLabel5.setText("Telefono");
        jPanel1.add(jLabel5);
        jPanel1.add(txtTel);

        jLabel4.setText("Direccion");
        jPanel1.add(jLabel4);
        jPanel1.add(txtDir);

        jLabel8.setText("Experiencia (años)");
        jPanel1.add(jLabel8);
        jPanel1.add(txtExperiencia);

        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.LINE_AXIS));

        jLabel7.setFont(new java.awt.Font("Pristina", 2, 36));
        jLabel7.setText("Instructores");

        jButton1.setText("Alta");
        jButton1.addActionListener(miContro_instructores);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Baja");
        jButton2.addActionListener(miContro_instructores);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Modificar");
        jButton3.addActionListener(miContro_instructores);

        jButton5.setText("Buscar");
        jButton5.addActionListener(miContro_instructores);
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton4.setText("Salir");
        jButton4.addActionListener(miContro_instructores);
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        pnlContenedor.setBackground(new java.awt.Color(153, 153, 153));

        javax.swing.GroupLayout pnlContenedorLayout = new javax.swing.GroupLayout(pnlContenedor);
        pnlContenedor.setLayout(pnlContenedorLayout);
        pnlContenedorLayout.setHorizontalGroup(
            pnlContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 508, Short.MAX_VALUE)
        );
        pnlContenedorLayout.setVerticalGroup(
            pnlContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 198, Short.MAX_VALUE)
        );

        btnMostrar.setText("Mostrar Todos");
        btnMostrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMostrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 88, Short.MAX_VALUE)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(293, 293, 293)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblEstado))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton1)
                        .addComponent(jButton2)
                        .addComponent(jButton3)
                        .addComponent(jButton5)
                        .addComponent(jButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnMostrar))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(pnlContenedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1)
                            .addComponent(jButton2)
                            .addComponent(jButton3)
                            .addComponent(jButton5)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton4)
                                .addComponent(btnMostrar)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pnlContenedor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblEstado)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:

        String nombre = txtNombre.getText();
        String paterno = txtApPat.getText();
        String materno = txtApMat.getText();
        String instituciones = txtInstituciones.getText();
        String colonia = txtColonia.getText();
        String telefono = txtTel.getText();
        String direccion = txtDir.getText();
        String experiencia = txtExperiencia.getText();
        pack();
        if(nombre.length() > 0 && paterno.length() > 0 && materno.length() > 0)
        {
          try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();         
        } catch( Exception e ) {
            JOptionPane.showMessageDialog(null,"No se pudo cargar el puente JDBC-ODBC.");
        }    
        try {
          con = DriverManager.getConnection("jdbc:mysql://localhost/Swim", "root", "abc321");
          stat = con.createStatement();
          stat.execute("INSERT INTO instructores Values('" + nombre + "','" + paterno +"','"+ materno +"','" + instituciones +"','" + colonia +"','" + telefono +"','" + direccion +"','" + experiencia +"')");
          con.close();
            JOptionPane.showMessageDialog(null, "Regisrado");
            //this.dispose();
        } catch (SQLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
        else
            lblEstado.setText("Error: Escribe los datos necesarios...");
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();         
        } catch( Exception e ) {
            JOptionPane.showMessageDialog(null,"No se pudo cargar el puente JDBC-ODBC.");
        }    
        try {
          con = DriverManager.getConnection("jdbc:mysql://localhost/Swim", "root", "abc321");
          stat = con.createStatement();
          String query = "SELECT * FROM instructores WHERE Nombre like '%" + txtNombre.getText() + "%'";
          rs = stat.executeQuery(query);
          DefaultTableModel modelo = new DefaultTableModel();
            
            //
            ResultSetMetaData metaDatos = rs.getMetaData();
            int cols = metaDatos.getColumnCount();
            Object[] etiquetas = new Object[cols];
            
            for (int i = 0; i < cols; i++)
            {
               etiquetas[i] = metaDatos.getColumnLabel(i + 1);
            }
            modelo.setColumnIdentifiers(etiquetas);
            
            while (rs.next())
            {
               // Se crea un array que será una de las filas de la tabla.
               Object [] fila = new Object[3];

               // Se rellena cada posición del array con una de las columnas de la tabla en base de datos.
               for (int i=0;i<3;i++)
                  fila[i] = rs.getObject(i+1);

               modelo.addRow(fila);
            }
            
            if(modelo.getRowCount() > 0)
                lblEstado.setText("Busqueda Realizada.");
            else
                lblEstado.setText("No se encontró ningun registro.");
            
            pnlContenedor.removeAll();
            pnlContenedor.repaint();
            JTable tblDatos = new JTable(modelo);
            tblDatos.setLocation(0, 100);
            pnlContenedor.add(tblDatos);
            pnlContenedor.repaint();
            
            pack();
            //
            //this.dispose();
        }catch(SQLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    }//GEN-LAST:event_jButton5ActionPerformed

    private void btnMostrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMostrarActionPerformed
        // TODO add your handling code here:
        
        try{
            String query = "SELECT * FROM instructores";
            rs = stat.executeQuery(query);
            DefaultTableModel modelo = new DefaultTableModel();
            pack();
            ResultSetMetaData metaDatos = rs.getMetaData();
            int cols = metaDatos.getColumnCount();
            Object[] etiquetas = new Object[cols];
            
            for (int i = 0; i < cols; i++)
            {
               etiquetas[i] = metaDatos.getColumnLabel(i + 1);
            }
            modelo.setColumnIdentifiers(etiquetas);
            
            // Bucle para cada resultado en la consulta
            while (rs.next())
            {
               // Se crea un array que será una de las filas de la tabla.
               Object [] fila = new Object[3]; // Hay tres columnas en la tabla

               // Se rellena cada posición del array con una de las columnas de la tabla en base de datos.
               for (int i=0;i<3;i++)
                  fila[i] = rs.getObject(i+1); // El primer indice en rs es el 1, no el cero, por eso se suma 1.

               modelo.addRow(fila);
            }
            
            if(modelo.getRowCount() > 0)
                lblEstado.setText("Listo");
            else
                lblEstado.setText("No se encontró ningun registro.");
            
            pnlContenedor.removeAll();
            pnlContenedor.repaint();
            JTable tblDatos = new JTable(modelo);
            tblDatos.setLocation(0, 100);
            pnlContenedor.add(tblDatos);
            pnlContenedor.repaint();
        }catch(SQLException er){
            lblEstado.setText(er.getMessage());
        }
        
    }//GEN-LAST:event_btnMostrarActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        Menu m1= new Menu();
        m1.show();
        this.dispose();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        
        String nombre = txtNombre.getText();
        String paterno = txtApPat.getText();
        String materno = txtApMat.getText();
        pack();
        if(nombre.length() > 0 && paterno.length() > 0 && materno.length() > 0)
        {
          try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();         
        } catch( Exception e ) {
            JOptionPane.showMessageDialog(null,"No se pudo cargar el puente JDBC-ODBC.");
        }    
        try {
          con = DriverManager.getConnection("jdbc:mysql://localhost/Swim", "root", "abc321");
          stat = con.createStatement();
          stat.execute("DELETE FROM instructores where ('" + nombre + "'= Nombre')");
          con.close();
            JOptionPane.showMessageDialog(null, "Regisrado");
            //this.dispose();
        } catch (SQLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
        else
            lblEstado.setText("Error: Escribe los datos necesarios...");
        
    }//GEN-LAST:event_jButton2ActionPerformed
    
    /**
     * @param args the command line arguments
     */
  
       
     
    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Instructores().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnMostrar;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JLabel lblEstado;
    private javax.swing.JPanel pnlContenedor;
    private javax.swing.JTextField txtApMat;
    private javax.swing.JTextField txtApPat;
    private javax.swing.JTextField txtColonia;
    private javax.swing.JTextField txtDir;
    private javax.swing.JTextField txtExperiencia;
    private javax.swing.JTextField txtInstituciones;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtTel;
    // End of variables declaration//GEN-END:variables
    
}
